﻿@using Smartstore.Web.Models.Cart;
@using Smartstore.Core.Checkout.Cart;

@model MiniShoppingCartModel

@{
    var descMaxLength = Model.ShowProductImages ? 100 : 140;
    var attrMaxLength = Model.ShowProductImages ? 50 : 70;
    var hasItems = Model.Items.Count > 0;

    var checkoutUrl = Model.AnonymousCheckoutAllowed && Model.CurrentCustomerIsGuest
        ? Url.RouteUrl("Login", new { checkoutAsGuest = true, returnUrl = Url.RouteUrl("ShoppingCart") })
        : Url.RouteUrl("Checkout");
}

<div class="offcanvas-cart-body offcanvas-scrollable">
    <div sm-if="!hasItems" class="no-items">
        <h4 class="no-item-title">
            @T("ShoppingCart.Mini.EmptyCart.Title")
        </h4>
        <p class="no-item-icon">
            <i class="fa fa-shopping-basket fa-4x"></i>
        </p>
        <p class="no-item-info text-muted">
            @Html.Raw(T("ShoppingCart.Mini.EmptyCart.Info", "fa fa-lg fa-cart-arrow-down"))
        </p>
    </div>

    @if (hasItems)
    {
        // Items added in the last 2 seconds are considered to be current.
        var currentItem = Model.Items.FirstOrDefault(x => x.CreatedOnUtc >= DateTime.UtcNow.AddSeconds(-2));

        <div sm-if="currentItem != null" class="alert alert-success alert-dismissible rounded-0 fade show" role="alert">
            <button type="button" class="btn-close" data-dismiss="alert" aria-label="Close"></button>
            <span sm-language-attributes-for="currentItem.ProductName">
                @Html.Raw(T("ShoppingCart.Mini.AddedItemToCart", $"<strong>{currentItem.ProductName.Value.Truncate(50, "…")}</strong>"))
            </span>
        </div>

        <div class="offcanvas-cart-items">
            @foreach (var item in Model.Items)
            {
                <div class="offcanvas-cart-item@(!item.Active ? " offcanvas-cart-item-inactive" : string.Empty)">
                    <div class="row sm-gutters">
                        <div sm-if="Model.ShowProductImages" class="col col-image col-alpha">
                            <a sm-if="item.Image.HasImage()" class="img-center-container" href="@item.ProductUrl" title="@item.Image.Title">
                                <img sm-model="item.Image" />
                            </a>
                        </div>
                        <div class="col col-data">
                            <a class="name fwm link-body link-dnu" href="@item.ProductUrl" title="@item.ProductName" sm-language-attributes-for="item.ProductName">@item.ProductName</a>

                            <div class="short-desc text-muted" sm-language-attributes-for="item.ShortDesc">
                                @Html.Raw(item.ShortDesc.Value.Truncate(descMaxLength, "…"))
                            </div>
                            <div sm-if="item.AttributeInfo.HasValue() || item.EssentialSpecAttributesInfo.HasValue()" class="cart-item-attrs small">
                                @Html.Raw(item.EssentialSpecAttributesInfo)
                                @Html.Raw(item.AttributeInfo)
                            </div>
                            <div sm-if="item.BundleItems.Any()" class="mb-bundle-pictures mt-3">
                                @for (int j = 0, bundlelength = item.BundleItems.Count; j < bundlelength; j++)
                                {
                                    var bundleItem = item.BundleItems[j];

                                    <a sm-if="bundleItem.ImageModel.HasImage()" href="@bundleItem.ProductUrl" title="@bundleItem.ProductName" class="link-dnn">
                                        <img sm-model="bundleItem.ImageModel" />
                                    </a>
                                    <i sm-if="(j + 1) < bundlelength" class="fa fa-plus"></i>
                                }
                            </div>

                            <zone name="offcanvas_cartitem_desc_after" model="item.Id"></zone>
                        </div>
                    </div>

                    <div class="row sm-gutters flex-wrap align-items-center mt-2">
                        <div class="col col-alpha">
                            @Html.EditorFor(x => item, "QtyInput", new
                            {
                                size = ControlSize.Small,
                                htmlAttributes = new
                                {
                                    data_update_url = Url.Action("UpdateCartItem", "ShoppingCart"),
                                    data_type = "cart",
                                    data_sci_id = item.Id,
                                }
                            })
                        </div>
                        <div class="col">
                            <span class="price unit-price">
                                @item.UnitPrice
                            </span>

                            @if (Model.ShowBasePrice && item.BasePriceInfo.HasValue())
                            {
                                <div class="pangv" attr-title="(item.BasePriceInfo.HasValue(), item.BasePriceInfo)">
                                    @Html.Raw(LocalizedValue.FixBrackets(item.BasePriceInfo, WorkContext.WorkingLanguage) ?? "&nbsp;")
                                </div>
                            }
                        </div>

                        <div class="col-auto text-nowrap">
                            @if (Model.DisplayMoveToWishlistButton)
                            {
                                <a class="btn btn-secondary btn-sm btn-icon ajax-cart-link" title="@T("ShoppingCart.MoveToWishlist")"
                                   href="#"
                                   rel="nofollow"
                                   data-href="@Url.Action("MoveItemBetweenCartAndWishlist", "ShoppingCart", new { cartItemId = item.Id, cartType = ShoppingCartType.ShoppingCart })"
                                   data-name="@item.ProductName"
                                   data-type="cart"
                                   data-action="addfromcart">
                                    <i class="fal fa-heart"></i>
                                </a>
                            }

                            <a class="btn btn-secondary btn-to-danger btn-sm btn-icon remove ajax-cart-link" title="@T("Common.Remove")"
                               href="#"
                               rel="nofollow"
                               data-href='@Url.Action("DeleteCartItem", "ShoppingCart", new { cartItemId = item.Id })'
                               data-name="@item.ProductName"
                               data-type="cart"
                               data-action="remove">
                                <i class="fal fa-trash-can"></i>
                            </a>
                        </div>
                    </div>
                </div>
            }
        </div>
    }

    <zone name="offcanvas_cart_items_after" />
</div>

<div sm-if="hasItems || Model.DisplayShoppingCartButton" class="offcanvas-cart-footer">
    <div class="offcanvas-cart-footer-row">
        @if (hasItems)
        {
            <div class="offcanvas-cart-footer-col mr-3">
                <div>
                    <div class="sub-total-caption">
                        @T("ShoppingCart.Totals.SubTotal"):
                    </div>
                    <div class="sub-total price">
                        @Model.SubTotal
                    </div>
                </div>
            </div>
            <div class="offcanvas-cart-footer-col row gy-1 gx-2 flex-wrap">
                @if (Model.DisplayCheckoutButton)
                {
                    <div class="col">
                        <a class="btn btn-success btn-flat btn-flat-light btn-block btn-action" asp-route="ShoppingCart">
                            @T("ShoppingCart.Mini.ViewCart")
                        </a>
                    </div>
                    <div class="col">
                        <a class="btn btn-clear btn-block btn-action" href="@checkoutUrl">
                            <i class="fa fa-check"></i>
                            <span>@T("Checkout.Button")</span>
                        </a>
                    </div>
                }
                else
                {
                    <div class="col">
                        <a class="btn btn-clear btn-block btn-action" asp-route="ShoppingCart">
                            <i class="fa fa-check"></i>
                            <span>@T("ShoppingCart.Mini.ViewCart")</span>
                        </a>
                    </div>
                }
            </div>
        }
        else if (Model.DisplayShoppingCartButton)
        {
            <a class="btn btn-clear btn-block btn-action" asp-route="ShoppingCart">
                <i class="fa fa-check"></i>
                <span>@T("ShoppingCart.Mini.ViewCart")</span>
            </a>
        }
    </div>

    <zone name="offcanvas_cart_footer_after" />
        
    @if (Model.DisplayCheckoutButton)
    {
        <div sm-suppress-if-empty-zone="offcanvas_cart_summary" class="offcanvas-cart-external-checkout">
            <div class="heading heading-center py-0">
                <h6 class="heading-title fs-h5 fwn">@T("Common.Or")</h6>
            </div>
            @*INFO: written like this so pseudo selector :not(:empty) can work and suppress padding, in case we need to return Empty() from ViewComponent directly*@
            <div class="d-flex justify-content-center align-items-center flex-wrap flex-column"><zone name="offcanvas_cart_summary" /></div>
        </div>
    }
</div>